#include <Misc.au3>
#include <ScreenCapture.au3>
#include <..\utility.au3>

HotKeySet("{PAUSE}", "theExit")
HotKeySet("9", "theExit")

call("screenshot", "start")

$numsweeps = IniRead("settings.ini", "run", "numsweeps", 1)

$stormarmor = 1 ;storm armor is slot 1
$explosion = 1 ;archon explosion is slot 1
$teleport = 2 ;teleport is slot 2
$magicweapon = 3 ;magic weapon is slot 3
$familiar = 4 ;familiar is slot 4

$goldpiles = 5 ;Number of gold piles to pick up

While 1 
	processkey('70', "mousePos") ;F1
	processkey('71', "leorics") ;F2
	processkey('72', "getLegendaries") ;F3
	processkey('73', "repair") ;F4
	processkey('74', "findring") ;F5
	processkey('75', "findcolor") ;F6
	processkey('76', "checkdead") ;F7
	processkey('77', "castSpelltest") ;F8
	processkey('78', "checkdisco") ;F9
WEnd

Func leorics()	
While 1
	Sleep($lagfactor)
	
	;Log Run Start
	call("logmessage", "Starting Run " & $runs)
	$runs = $runs + 1
	
	;Randomly take a break
	If $runs > 10 Then
		call("takebreak")
	EndIf
	
	;Repair if needed
	call("repair")
	
	;Move to portal
	call("fuzzyclick","left", 113, 24)
	
	Local $mode = Random(1,6,1)
	If $mode = 1 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
	ElseIf $mode = 2 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
	ElseIf $mode = 3 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
	ElseIf $mode = 4 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
	ElseIf $mode = 5 Then
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
	Else
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $familiar)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $magicweapon)
		call("fuzzysleep",Random(400,500,1))
		call("castSpell", $stormarmor)
	EndIf
	
	
	call("fuzzysleep",4600 * $runspeed)
	Sleep($lagfactor)
	
	;Get shrine
	;call("getLhoricShrine")
	
	
	
	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	;Move to stairs
	;call("fuzzyclick","left", 0, 0)
	;call("fuzzysleep",2000 * $runspeed)
	
	;Teleport to door
	;call("fuzzymove",150,0)
	
	;Opt("SendKeyDownDelay", Random(1200,1300,1))
	;Send("2")
	;Opt("SendKeyDownDelay", 50)  ;reset to default when done
	
	;Keep walking to door
	;call("fuzzyclick","left", 0, 0)
	;call("fuzzysleep",600)
	
	;Enter door
	;call("fuzzyclick","left", 993, 311)
	;call("fuzzysleep",Random(2200,2400))
	;If call("enterdoor") = 0 Then
	;	call("exitgame", 0)
	;	ContinueLoop
	;EndIf
	
	;Sleep($lagfactor)
	
	;Move down aisle ; 243,32
	;call("fuzzyclick","left", 230, 0)
	;call("fuzzysleep",100 * $runspeed)
	;call("fuzzyclick","left", 230, 0)
	;call("fuzzysleep",3600 * $runspeed)
	
	;Check if we are already dead
	;If call("checkdead", 0) = 1 Then
		;If we are in manual mode, then see if there are any legendaries
	;	If $manualpickup = 1 Then
			;Pick up Legendaries
	;		call("getLegendaries", 0)
	;	EndIf
		
	;	call("exitgame", 0)
	;	ContinueLoop
	;EndIf
	
	
	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	
	
	call("fuzzymove",1601,755)
	Opt("SendKeyDownDelay", Random(1300,1400,1))
	Send("2")
	Opt("SendKeyDownDelay", 50)  ;reset to default when done
	
	Send("{CTRLDOWN}")
	call("fuzzysleep", Random(1000,1200))
	call("fuzzyclick","left",960,850)
	call("fuzzysleep", Random(400,600))
	call("fuzzyclick","left",960,850)
	call("fuzzysleep", Random(400,600))
	call("fuzzyclick","left",960,850)
	call("fuzzysleep", Random(400,600))
	call("fuzzyclick","left",960,850)
	call("fuzzysleep", Random(400,600))
	call("fuzzyclick","left",960,850)
	call("fuzzysleep", Random(400,600))
	call("fuzzyclick","left",960,850)
	call("fuzzysleep", Random(400,600))
	call("fuzzyclick","left",960,850)
	call("fuzzysleep", Random(400,600))
	call("fuzzyclick","left",960,850)
	call("fuzzysleep", Random(400,600))
	call("fuzzyclick","left",960,850)
	call("fuzzysleep", Random(400,600))
	Send("{CTRLUP}")
	
	;Electrocute for 10~ish seconds
	Send("{SHIFTDOWN}") ;press and hold shift down
	call("fuzzymove",960, 540)
	MouseDown("left")
	
	;Fire at remaining guys
	For $i=0 to 5
		If call("checkdead", 0) = 1 Then
			ExitLoop
		EndIf
		
		Local $finalmobs = PixelSearch(400,0,1400,700,0xF70000,15)
		if IsArray($finalmobs) Then
			call("fuzzymove", $finalmobs[0],$finalmobs[1]+20)
			call("fuzzysleep",Random(1800,2400,1))
		Else
			ExitLoop
		EndIf
	Next
	
	MouseUp("left")
	Send("{SHIFTUP}") ;let go of the shift button 
	
	;Pick up Legendaries
	call("getLegendaries", 0)
	
	
	;Wait for cooldown
	Sleep(1500)

	
	
	
	
	
	
	
	
	
	call("fuzzyclick","left",1557,245)
	call("fuzzysleep", Random(1000,1200))
	call("fuzzyclick","left",1557,145)
	call("fuzzysleep", Random(1000,1200))
	
	call("fuzzymove",1651,758)
	Opt("SendKeyDownDelay", Random(1300,1400,1))
	Send("2")
	Opt("SendKeyDownDelay", 50)  ;reset to default when done
	Sleep(1000)
	
	
	
	
	
	
	
	;Fire forward
	call("fuzzymove", 455,109)
	MouseDown("right")
	call("fuzzysleep",Random(2000,3000,1))
	;
	;For $i=1 to $numsweeps
	;	If Random(1,2,1) = 1 Then
	;		;Fire left
	;		call("fuzzymove", 241,357)
	;		call("fuzzysleep",Random(1500,2500,1))
	;		call("castSpell", $explosion)
	;
	;		;Fire right
	;		call("fuzzymove", 932,192)
	;		call("fuzzysleep",Random(1500,2500,1))
	;		call("castSpell", $explosion)
	;	Else
	;		;Fire Right
	;		call("fuzzymove", 932,192)
	;		call("fuzzysleep",Random(1500,2500,1))
	;		call("castSpell", $explosion)
	;	
	;		;Fire Left
	;		call("fuzzymove", 241,357)
	;		call("fuzzysleep",Random(1500,2500,1))
	;		call("castSpell", $explosion)
	;	EndIf
	;
	;
	;	;Explosion
	;	call("castSpell", $explosion)
	;
	;	;Fire forward
	;	call("fuzzymove", 455,109)
	;	call("fuzzysleep",Random(1500,2000,1))
	;	call("castSpell", $explosion)
	;
	;Next
	;
	;;Release archon beam
	;MouseUp("right")
	;
	;
	;;Pick up Legendaries
	;call("getLegendaries", 0)
	;
	;Sleep(50)
	
	;Fire at remaining guys
	For $i=0 to 8
		If call("checkdead", 0) = 1 Then
			ExitLoop
		EndIf
		
		;Press 1 on third loop
		If $i = 3 Then
			Send("1")
		EndIf
		
		Local $finalmobs = PixelSearch(400,0,1400,700,0xF70000,15)
		if IsArray($finalmobs) Then
			MouseDown("right")
			call("fuzzymove", $finalmobs[0],$finalmobs[1]+20)
			call("fuzzysleep",Random(1800,2400,1))
			MouseUp("right")
			call("fuzzysleep", 200)
			call("castSpell", $explosion)
		Else
			ExitLoop
		EndIf
	Next
	
	;Pick up Legendaries
	call("getLegendaries", 0)
	
	;Check if we are dead and just exit
	If call("checkdead", 0) = 1 Then
		call("exitgame", 0)
		ContinueLoop
	EndIf
	
	call("castSpell", $explosion)
	
	;Pick up rare rings
	If call("findring") = 1 Then
		call("findring")
	EndIf
	
	;Pick up gold
	call("pickupGold", $goldpiles)
	
	call("exitgame")
	
	;Restart Script
WEnd	
	
EndFunc

Func enterdoor()
	Local $color = 0xFBFB89
	
	$extralag = $lagfactor / 100
	
	;Enter door
	call("fuzzyclick","left", 993, 311)
	For $j=0 to (20 + $extralag)
		Sleep(100)
			
		;Did we enter the manor?
		If PixelGetColor(725,609) = 0xFFFFFF AND PixelGetColor(804,610) = 0xFFFFFF AND PixelGetColor(1146,611) = 0xFFFFFF Then
			call("logmessage", "Entered Leoric's Manor.")
			Return 1
		EndIf
	Next
	
	For $i=0 to 3
		;Exit early if we are dead
		If call("checkdead", 0) = 1 Then
			Return 0
		EndIf
		
		Local $arr = PixelSearch(0,0,960,540,$color, 10)
		If IsArray($arr) Then
			call("fuzzyclick", "left", $arr[0], $arr[1])
			call("fuzzysleep", 1000)
			
			For $j=0 to (20 + $extralag)
				Sleep(100)
			
				;Did we enter the manor?
				If PixelGetColor(725,609) = 0xFFFFFF AND PixelGetColor(804,610) = 0xFFFFFF AND PixelGetColor(1146,611) = 0xFFFFFF Then
					call("logmessage", "Entered Leoric's Manor.")
					Return 1
				EndIf
			Next
			
			;MsgBox(0,"test","test")
			
		EndIf
		
		;MsgBox(0,"test2","test2")
		;Keep moving up and left
		call("fuzzyclick", "left", 200, 0)
		Sleep(500)
	Next
	
	
	
	;We didn't, need to restart game
	call("logmessage", "FAILED TO ENTER Leoric's Manor. Exiting game.")
	call("screenshot", "doorfailure")
	Return 0
EndFunc